'use strict';

ksrBusinessAcctControllers.controller('eventsOverviewCtrl', ['$scope', 'ksrCacheSvc',
	 function($scope, ksrCacheSvc) {
	   console.log("eventsOverviewCtrl");
	   $scope.breadcrumbs=[
	   					 {"name":"Account","url":"#/overview"}
	                  	,{"name":"Events","url":""}
	                ];
	    ksrCacheSvc.get("eventList",false).then(function(data){
	    	$scope.events = data;
	    	if ($scope.events.length>0){
	    		$scope.business = $scope.events[0].business;
	    	} else { //if no events, fetch business
	    		 ksrCacheSvc.get('business',true).then(function(data){
			    	$scope.business = data;
			    })	
	    	}
	    })
 }]);

ksrBusinessAcctControllers.controller('manageEventCtrl', ['$scope', '$routeParams','ksrCacheSvc',
    function($scope, $routeParams,ksrCacheSvc) {
	console.log("manageEventCtrl");
	$scope.breadcrumbs=[
						{"name":"Account","url":"#/overview"}
	                    ,{"name":"Events","url":"#/events-overview"}
	                    ,{"name":"Manage Event","url":""}
	                    ];
	ksrCacheSvc.getItemById($routeParams.eventId,"eventList",true).then(function(data){
   		$scope.event = data;
		ksrCacheSvc.get("eventScheduleList",false,$scope.event).then(function(data){
		    	$scope.schedules = data;
		})
   })
}]);

ksrBusinessAcctControllers.controller('eventCtrl',  ['$scope', '$routeParams','$location','ksrCacheSvc','jsonSvc',
    function($scope, $routeParams,$location,ksrCacheSvc,jsonSvc) {
    console.log("eventCtrl");
	if ($routeParams.eventId){
			$scope.breadcrumbs=[
			                    {"name":"Account","url":"#/overview"}
	                    		,{"name":"Events","url":"#/events-overview"}
			                    ,{"name":"Manage Event","url":"#/manage-business-event/"+$routeParams.eventId}
			                    ,{"name":"event","url":""}
			                    ];
		   console.log("editEvent="+$routeParams.eventId);
		   ksrCacheSvc.getItemById($routeParams.eventId,"eventList",true).then(function(data){
		   		$scope.event = data;
		   		$scope.mode="edit";
		   })
	   } else {
			$scope.breadcrumbs=[
	                    {"name":"Account","url":"#/overview"}
	                    ,{"name":"Events","url":"#/events-overview"}
	                    ,{"name":"new event","url":""}
	                    ];	   	
		   console.log("new event");
		   $scope.event = {categories:[]};
		   ksrCacheSvc.get("business",true).then(function(data){
	    		$scope.event.business = data;
	    	})	  
		   $scope.mode="Create";
	   }
	   $scope.submit=function(){
			jsonSvc.saveItem({entityType:"business",type:"event"},$scope.event).$promise.then(function(data){
				console.log("udpate business status:"+data.status);
				if ($scope.mode==="edit"){
					$location.path("/manage-business-event/"+$routeParams.eventId);
				} else {
					$location.path("#/overview");
				}
			});
	   }

}]);

ksrBusinessAcctControllers.controller('eventScheduleCtrl', ['$scope', '$routeParams','$location','ksrCacheSvc','jsonSvc',
    function($scope, $routeParams,$location,ksrCacheSvc,jsonSvc) {
	console.log("eventScheduleCtrl");
	$scope.breadcrumbs=[
	                    {"name":"Account","url":"#/overview"}
	                   	,{"name":"Events","url":"#/events-overview"}
	                    ,{"name":"Manage Event","url":"#/manage-business-event/"+$routeParams.eventId}
	                    ,{"name":"schedule","url":""}
	                    ];
	 if ($routeParams.eventId){
		ksrCacheSvc.getItemById($routeParams.eventId,"eventList",true).then(function(data){
	   		if ($routeParams.scheduleId){
				ksrCacheSvc.getItemById($routeParams.scheduleId,"eventScheduleList",true,data).then(function(data){
			    	$scope.schedule = data;
			    	// $scope.addAddress();
			    	$scope.mode="edit";
				}) 
			
			} else {
				$scope.schedule = {event:data,eventAddresses:[]};
				// $scope.addAddress();
				$scope.mode="new";
			}
	   })
	 	
	 }
	 $scope.addAddress=function(){
	 	console.log("add an address!");
	 	angular.forEach($scope.schedule.eventAddresses, function(add){
	 		add.active=false;
	 	})
	 	$scope.schedule.eventAddresses.push({
    		addressLine1:"",
    		addressLine2:"",
    		city:"",
    		state:"",
    		zip:"",
    		active:true
    	})
	 }

	 $scope.submit=function(){
			jsonSvc.saveItem({entityType:"business",type:"event-schedule"},$scope.schedule).$promise.then(function(data){
				console.log("udpate event-schedule status:"+data.status);
				$location.path("/manage-business-event/"+$routeParams.eventId);

			});
	   }
}]);

ksrBusinessAcctControllers.controller('eventPhotoCtrl', ['$scope', '$routeParams','$location','ksrCacheSvc','jsonSvc',
    function($scope, $routeParams,$location,ksrCacheSvc,jsonSvc) {
	console.log("eventPhotoCtrl");
	$scope.breadcrumbs=[
	                    {"name":"Events manage","url":"#/overview"}
	                    ,{"name":"overview","url":""}
	                    ];
}]);


